/**
 * @api {get} /api/admin/v1/suggestions/browse 建议列表
 * @apiGroup AdminSuggestions
 * @apiDescription 建议管理-建议列表
 * @apiPermission 管理员(查看建议权限)
 * @apiVersion 1.0.0
 */
exports.browse = function ({query}, {models: {Suggestions, Users, associations}, controller}) {
    return Suggestions.findAndCountAll(controller.pagination({
        params: query,
        filters: ['type', 'status', 'user_id', 'feature'],
        likes: ['tel', 'qq', 'content']
    })).then(function (data) {
        return associations.relateOne(data, Users, 'user_id', 'user', ['id', 'openid', 'image', 'username', 'nickname', 'tel']);
    });
}

/**
 * @api {get} /api/admin/v1/suggestions/load 建议详情
 * @apiGroup AdminSuggestions
 * @apiDescription 建议管理-建议详情
 * @apiPermission 管理员(查看建议权限)
 * @apiVersion 1.0.0
 */
exports.load = function ({query}, {models: {Suggestions}}) {
    return Suggestions.findOne({where: {id: query.id}});
}

/**
 * @api {post} /api/admin/v1/suggestions/add 建议添加
 * @apiGroup AdminSuggestions
 * @apiDescription 建议管理-建议添加
 * @apiPermission 管理员(添加建议权限)
 * @apiVersion 1.0.0
 */
exports.add = function ({body}, {models: {Suggestions}}) {
    return Suggestions.create(body).then(function (data) {
        return {success: true, data: data};
    });
}

/**
 * @api {post} /api/admin/v1/suggestions/edit 建议编辑
 * @apiGroup AdminSuggestions
 * @apiDescription 建议管理-建议编辑
 * @apiPermission 管理员(编辑建议权限)
 * @apiVersion 1.0.0
 */
exports.edit = function ({body}, {models: {Suggestions}}) {
    let {id} = body;
    delete body.id;
    return Suggestions.update(body, {where: {id: id}}).then(function (result) {
        if (!result[0]) {
            return {success: false};
        }
        return Suggestions.findOne({where: {id: id}}).then(function (suggestion) {
            return {success: true, data: suggestion};
        });
    });
}

/**
 * @api {post} /api/admin/v1/suggestions/delete 建议删除
 * @apiGroup AdminSuggestions
 * @apiDescription 建议管理-建议删除
 * @apiPermission 管理员(删除建议权限)
 * @apiVersion 1.0.0
 */
exports.delete = function ({body}, {models: {Suggestions}}) {
    let {id} = body;
    return Suggestions.destroy({where: {id: id}}).then(function (count) {
        return {success: (count > 0)};
    });
}